<?php

namespace app\common\business;

use app\common\lib\Arr;
use app\common\model\mysql\ScreenTalent;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;

class Talent {

    public $model = null;

    public function __construct()
    {
        $this->model = new ScreenTalent();
    }

    /**
     * 获取分页列表
     * @throws Exception
     */
    public function getTableList($page=null,$limit=null,$keyword=''): array
    {
        $where=[
            ['name','like','%'.$keyword.'%']
        ];
        $data=$this->model->getTableList($where,$page,$limit);
        $category=(new Category())->categoryDictionaries();
        $talentCategory=Constant::$TalentCategory;
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['gender_text']= Constant::$Gender[$v['gender']] ?? '-';
            $data['list'][$k]['nation_text']= $category[$v['nation']] ?? '-';
            $data['list'][$k]['politics_text']= $category[$v['politics']] ?? '-';
            $data['list'][$k]['technology_text']= $category[$v['technology']] ?? '-';
            $data['list'][$k]['education_text']= $category[$v['education']] ?? '-';
            $data['list'][$k]['degree_text']= $category[$v['degree']] ?? '-';
            $data['list'][$k]['birthday']=!empty($v['birthday'])?date('Y-m-d',$v['birthday']):'';
            $data['list'][$k]['graduation_time']=!empty($v['graduation_time'])?date('Y-m-d',$v['graduation_time']):'';
            $data['list'][$k]['technical_position_time']=!empty($v['technical_position_time'])?date('Y-m-d',$v['technical_position_time']):'';
            $data['list'][$k]['related_profession']=empty($v['related_profession'])?[]:Arr::toNumber(explode(',',$v['related_profession']));
            $data['list'][$k]['talent_category']=empty($v['talent_category'])?[]:Arr::toNumber(explode(',',$v['talent_category']));
            $data['list'][$k]['titles']=empty($v['titles'])?[]:Arr::toNumber(explode(',',$v['titles']));
            $talentCategoryText=[];
            foreach ($data['list'][$k]['talent_category'] as $item) {
                $talentCategoryText[]=$talentCategory[$item];
            }
            $data['list'][$k]['talent_category_text']=implode(',',$talentCategoryText);
        }
        return $data;
    }

    /**
     * 新增/保存数据
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException|Exception
     */
    public function save($data): bool
    {
        if(empty($data['id'])){
            unset($data['id']);
        }
        $data=$this->formattingSaveData($data);
        return $this->model->saveData($data);
    }

    /**
     * 根据id删除
     * @param $id
     * @return bool
     * @throws Exception
     */
    public function deleteById($id): bool
    {
        if(empty($id)){
            throw new Exception('参数错误');
        }
        return $this->model->deleteById($id);
    }

    /**
     * 根据id修改状态
     * @param $data
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function updateStatus($data): bool
    {
        if(empty($data['id'])){
            throw new Exception('参数错误');
        }
        return $this->model->updateStatus($data);
    }

    private function formattingSaveData($data){
        $data['degree']=!empty($data['degree'])?$data['degree']:0;
        $data['level']=!empty($data['level'])?$data['level']:0;
        $data['unit_type']=!empty($data['unit_type'])?$data['unit_type']:0;
        $data['unit_nature']=!empty($data['unit_nature'])?$data['unit_nature']:0;
        $data['technology']=!empty($data['technology'])?$data['technology']:0;
        $data['introduce_area']=!empty($data['introduce_area'])?$data['introduce_area']:0;
        $data['birthday']=empty($data['birthday'])?null:strtotime($data['birthday']);
        $data['graduation_time']=empty($data['graduation_time'])?null:strtotime($data['graduation_time']);
        $data['technical_position_time']=empty($data['technical_position_time'])?null:strtotime($data['technical_position_time']);
        $data['related_profession']=implode(',',$data['related_profession']);
        $data['talent_category']=implode(',',$data['talent_category']);
        $data['titles']=implode(',',$data['titles']);
        return $data;
    }
}
